//
//  SJPDepartmentController.m
//  SWS_Project00
//
//  Created by 马继鵬 on 17/3/8.
//  Copyright © 2017年 SWS. All rights reserved.
//

#import "SJPDepartmentController.h"
#import "SJPDepatmentModel.h"
#import "SJPDepartmentCell.h"
#import "SJPDepartmentViewController.h"
#import "SJPBaseViewController.h"

@interface SJPDepartmentController ()
@property (strong,nonatomic) NSArray<SJPDepatmentModel*>* departmentList;
@end

@implementation SJPDepartmentController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self.collectionView registerClass:[SJPDepartmentCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.collectionView.backgroundColor = [UIColor grayColor];
    [self loadData];
    UIImage* backImage = [UIImage imageNamed:@"jiantou"];
   self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:nil];
    
}
#pragma
#pragma-数据加载
- (void)loadData
{
    NSString* path = [[NSBundle mainBundle]pathForResource:@"departments.plist" ofType:nil];
    NSArray* arr = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray* arrM = [NSMutableArray array];
    for (NSDictionary *dict in arr )
    {
        SJPDepatmentModel *model = [[SJPDepatmentModel alloc]init];
        [model setValuesForKeysWithDictionary:dict];
        [arrM addObject:model];
    }
    _departmentList = arrM.copy;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>
#pragma
#pragma-数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _departmentList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   SJPDepartmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor whiteColor];
    // Configure the cell
    cell.model = self.departmentList[indexPath.item];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>
#pragma
#pragma-代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SJPDepatmentModel* model = _departmentList[indexPath.item];
    UIImageView* backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 15)];
    UIImage* backImage = [UIImage imageNamed:@"jiantou"];
    backView.image = [UIImage imageNamed:@"jiantou"];
    if (model.key)
    {
        SJPBaseViewController* emptycontroller = [SJPBaseViewController new];
        emptycontroller.model = model;
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:nil];
        [self.navigationController pushViewController:emptycontroller animated:YES];
    }else{
        SJPDepartmentViewController* controller = [SJPDepartmentViewController new];
        controller.model = model;
         controller.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backView];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

@end
